package com.xiaoKe.zhonghzu.global.base;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.xiaoKe.zhonghzu.R;
import com.xiaoKe.zhonghzu.global.bean.GeneralData;
import com.xiaoKe.zhonghzu.global.dialog.ToastDialog;
import com.xiaoKe.zhonghzu.global.okhttp.GsonSerializer;
import com.xiaoKe.zhonghzu.global.tool.DateUtils;
import com.xiaoKe.zhonghzu.global.tool.HideIMEUtil;
import com.xiaoKe.zhonghzu.global.tool.JsonUtils;
import com.xiaoKe.zhonghzu.global.tool.SPUtils;
import com.xiaoKe.zhonghzu.global.tool.ToastUtils;
import com.xiaoKe.zhonghzu.global.view.ViewTitle;
import com.xiaoKe.zhonghzu.ui.dialog.HintDialog;
import com.yzq.zxinglibrary.common.Constant;


import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static android.app.Activity.RESULT_OK;

/**
 * @ProjectName : xiaoke-pad-zhongzhu
 * @Author : Shijingna
 * @Time : 2024/7/15 16:53
 * @Description : 基类
 */
public abstract class BaseFragment<T extends ViewBinding> extends Fragment {
    public T binding;
    public BaseRefreshLayout baseRefreshLayout;
    public  ToastDialog toastDialog;
    public TextView name;

    public List<String> permissionList = SPUtils.getList("permissionList");

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Type superclass = getClass().getGenericSuperclass();
        Class<T> aClass = (Class<T>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
        try {
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class);
            binding = (T) method.invoke(null, getLayoutInflater());
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        if (null == binding) {
            return null;
        }

        return binding.getRoot();
    }

    public BroadcastReceiver resultReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            getReceiverData(intent);
        }
    };

    /***
     * 调用子类方法
     * */
    public void getReceiverData(Intent intent) {

    }

    @Override
    public void onResume() {
        super.onResume();
        //app检查版本更新
//        if (!BaseApplication.isIsUpPop()){ //已经弹窗了就不需要在检查了，等弹窗结束了，再继续检查
//            new VersionsModel().getVersion(getContext(), getChildFragmentManager(),false);
//        }

    }

    @SuppressLint("UseRequireInsteadOfGet")
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        HideIMEUtil.wrap(Objects.requireNonNull(getActivity()));//键盘管理，点击除editText外区域收起键盘
        initTitle();
        initView();
        initData();
        baseRefreshLayout = getView().findViewById(R.id.smartRefreshLayout);
    }

    ViewTitle viewTitle;
    TextView titleName;

    protected void initTitle() {
        if (null == getView()) {
            return;
        }
        toastDialog = new ToastDialog();
        viewTitle = getView().findViewById(R.id.viewTitle);
        titleName = getView().findViewById(R.id.titleName);

        if (null != viewTitle) {
            viewTitle.initTitle(getArguments().getString("titleName"), getArguments().getString("titleNexName"), getActivity());
        }

        if (null!=titleName){
            titleName.setText(getArguments().getString("title"));
        }

        name = getView().findViewById(R.id.titleNexName);
        if (getArguments() != null) {
            String houseName = getArguments().getString("name");
            if (!TextUtils.isEmpty(houseName)) {
                if (null != name) {
                    name.setVisibility(View.VISIBLE);
                    name.setText(houseName);
                }
            }
        }

    }

    protected abstract void initView();

    protected abstract void initData();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    /**
     * @param routerConfig
     */
    public void toFragmentPage(RouterConfig routerConfig) {
        AppRouter.toFragment(getContext(), routerConfig);
    }

    /**
     * @param routerConfig
     * @param requestCode
     */
    protected void toFragmentForResult(RouterConfig routerConfig, int requestCode) {
        AppRouter.toFragmentForResult(this, routerConfig, requestCode);
    }

    public void finish(boolean aBoolean) {
        if (!aBoolean) {
            HintDialog hintDialog = new HintDialog();
            hintDialog.setData("", getString(R.string.确定要返回上一页面么));
            hintDialog.setOnOtherListener(new OnOtherListener() {
                @Override
                public void onOther(String... content) {
                    switch (content[0]) {
                        case "affirm":
                            getActivity().finish();
                            break;
                    }
                }
            });
            hintDialog.show(getChildFragmentManager(), "");
        } else {
            getActivity().finish();
        }
    }


}
